﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Benefit.Service.ServerData
{
    /// <summary>
    /// 平仓明细
    /// </summary>
    public class ServiceT_ClosePositionDetail_History : Benefit.Interface.ServerData.IT_ClosePositionDetail_History
    {
        Benefit.DB.DBManager db = null;
        public ServiceT_ClosePositionDetail_History(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 查询结算后的平仓明细
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_ClosePositionDetail_History> GetInfo(string pdate)
        {
            List<Models.ServerData.T_ClosePositionDetail_History> ios = new List<Models.ServerData.T_ClosePositionDetail_History>();
            Interface.ServerData.IT_Settle_History icdh = new ServerData.ServiceT_Settle_History(db);
            //T_Settle_History tsh = new T_Settle_History();
            int settlementid = icdh.GetDayMaxSettlementInfoId(pdate);
            if (settlementid != -1)
            {
                ios = GetClosePositionDetailHistoryInfo(settlementid, pdate);
            }
            return ios;
        }

        /// <summary>
        /// 查询某天最大结算编号的出入金记录
        /// </summary>
        /// <param name="settlementid">结算编号</param>
        /// <param name="pdate">日期</param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_ClosePositionDetail_History> GetClosePositionDetailHistoryInfo(int settlementid, string pdate)
        {
            DavidDB.ADO.DBManager dbmanager = new DavidDB.ADO.DBManager();
            System.Collections.Generic.List<Models.ServerData.T_ClosePositionDetail_History> ios = new System.Collections.Generic.List<Models.ServerData.T_ClosePositionDetail_History>();
            string sql = "select * from T_ClosePositionDetail_History where SettlementId='" + settlementid + "' and tradingday='" + pdate + "'";
            dynamic sdr = dbmanager.ExecuteReader(sql);
            while (sdr.Read())
            {
                if (Convert.ToInt32(sdr["Volume"]) > 0)
                {
                    Models.ServerData.T_ClosePositionDetail_History s = null;
                    s = new Models.ServerData.T_ClosePositionDetail_History();
                    s.AccountId = Convert.ToInt32(sdr["AccountId"]);
                    s.CloseDate = Convert.ToInt32(sdr["CloseDate"]);
                    s.CloseProfit = Convert.ToDouble(sdr["CloseProfit"]);
                    s.CloseProfit2 = Convert.ToDouble(sdr["CloseProfit2"]);
                    s.DeliveryDate = sdr["DeliveryDate"].ToString();
                    s.Direction = Convert.ToString(sdr["Direction"]);
                    s.ExchangeId = Convert.ToString(sdr["ExchangeId"]);
                    s.Id = Convert.ToInt32(sdr["Id"]);
                    s.InstrumentId = Convert.ToString(sdr["InstrumentId"]);
                    s.OpenDate = Convert.ToInt32(sdr["OpenDate"]);
                    s.OpenPrice = Convert.ToDouble(sdr["OpenPrice"]);
                    s.ProductId = Convert.ToString(sdr["ProductId"]);
                    s.SettlementId = Convert.ToInt32(sdr["SettlementId"]);
                    s.TradePrice = Convert.ToDouble(sdr["TradePrice"]);
                    s.TradingDay = Convert.ToString(sdr["TradingDay"]);
                    s.Volume = Convert.ToInt32(sdr["Volume"]);
                    s.YesterdaySettlement = Convert.ToDouble(sdr["YesterdaySettlement"]);
                    ios.Add(s);
                }
            }
            sdr.Close();
            dbmanager.Commit();
            dbmanager.Dispose();
            return ios;
        }

        /// <summary>
        /// 每天都需要执行的把Server中的数据导入到分析系统中的方法
        /// </summary>
        /// <param name="pdate">日期</param>
        public void CopyServerClosePositionDetailHistoryDataIntoBenefit(string pdate)
        {
            Interface.ServerData.IT_ClosePositionDetail_History icph = new ServerData.ServiceT_ClosePositionDetail_History(db);
            //Models.ServerData.T_ClosePositionDetail_History h = new Models.ServerData.T_ClosePositionDetail_History();
            List<Models.ServerData.T_ClosePositionDetail_History> historys = icph.GetInfo(pdate);
            foreach (Models.ServerData.T_ClosePositionDetail_History ha in historys)
            {
                db.ClosePositionDetail.Add(ha);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 删除某日的所有出入金记录
        /// </summary>
        /// <param name="pdate"></param>
        public void DeleteDayClosePositionDetailHistoryFromBenefit(string pdate)
        {
            var historys = from t in db.ClosePositionDetail where t.TradingDay.Equals(pdate) select t;
            foreach (Models.ServerData.T_ClosePositionDetail_History ha in historys)
            {
                db.ClosePositionDetail.Remove(ha);
            }
            db.SaveChanges();
        }

    }
}